// pages/user/user.js
const app = getApp();
Page({

  /** 
   * 页面的初始数据 
   */
  data: {
    showModal: false,
    msg: '点我登录',
    userInfo: null,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    showModal: false,
  },
  onShow: function() {
    var me = this;
    //判断是否已登录
    wx.getStorage({
      key: 'userid',
      success: function(res) {
        console.log('成功拿到userid')
        me.setData({
          msg: '您已登录'
        })
      },
      fail: function(res) {
        wx.showModal({
          title: '您未登录',
          content: '请点击登录按钮去登录！',
          showCancel: true,
          cancelText: '取消',
          confirmText: '登录',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateTo({
                url: '/pages/login/login',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          }
        })
      }
    })



  },
  onLoad: function() {
    var me = this;
    var childs = me.data.childs;
    var phoneNo;
    wx.getStorage({
      key: 'childcode',
      success: function(res) {

      },
      fail: function() {
        wx.getStorage({
          key: 'userid',
          success: function(res) {
            console.log('childlist拿到userid')
            console.log(res.data)
            phoneNo = res.data;
            wx.request({
              url: app.serverUrl + '/showChildrenList',
              method: "Post",
              data: {
                userid: phoneNo
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
              success: function(res) {
                console.log(res.data)
                var childInfo = res.data.data;
                console.log(childInfo);
                var current = current;
                if (childInfo.length == 0) {
                 
                } else {
                  // 将current赋值为当前列表第一个孩子，并缓存起来
                  var current = childInfo[0].childname + childInfo[0].childcode;
                  var childcode = childInfo[0].childcode;
                  wx.setStorage({
                    key: 'childcode',
                    data: childcode,
                  })
                  console.log('第一个孩子' + current)
                  me.setData({
                    current: current
                  })
                }
              }
            })
          }
        })
      }
    })

  },

  bindGrow: function() {
    wx.getStorage({
      key: 'childcode',
      success: function(res) {
        console.log('拿到缓存中的childcode')
        console.log(res.data)
        var childcode = res.data;
        wx.navigateTo({
          url: '/pages/grow/grow',
        })
      },
      fail: function() {
        wx.showToast({
          title: '请先绑定宝贝',
          icon: 'none'
        })
      }
    })

  },
  bindMake: function() {
    wx.getStorage({
      key: 'childcode',
      success: function(res) {
        console.log('拿到缓存中的childcode')
        console.log(res.data)
        var childcode = res.data;
        wx.navigateTo({
          url: '/pages/make/make',
        })
      },
      fail: function() {
        wx.showToast({
          title: '请先绑定宝贝',
          icon: 'none'
        })
      }
    })
  },
  bindRest: function() {
    // wx.navigateTo({
    //   url: '/pages/rest/rest',
    // })
    wx.showToast({
      title: '施工中...',
      icon: 'loading',
      duration: 1000
    })
  },
  bindChange: function() {

    wx.navigateTo({
      url: '/pages/change/change',
    })
  },
  bindKid: function() {
    var me = this;
    var phoneNo;
    wx.getStorage({
      key: 'userid',
      success: function(res) {
        console.log('childlist拿到userid')
        console.log(res.data)
        phoneNo = res.data;
        wx.request({
          url: app.serverUrl + '/showChildrenList',
          method: "Post",
          data: {
            userid: phoneNo
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function(res) {
            console.log(res.data)
            var childInfo = res.data.data;
            console.log(childInfo);
            if (childInfo.length == 0) {
              wx.showToast({
                title: '请先绑定宝贝',
                icon: 'none',
                duration: 1000
              })
              wx.navigateTo({
                url: '/pages/kid/kid',
              })
            } else {
              wx.navigateTo({
                url: '/pages/childList/childList',
              })
            }
          }
        })
      }
    })
  },
  //退出登录
  bindOut: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认退出登录吗？',
      success(res) {
        if (res.confirm) {
          console.log(res.data)
          wx.clearStorage({
            success: function(res) {
              app.userInfo = null;
              wx.showToast({
                title: '退出成功',
                icon: 'none',
                duration: 2000
              });
              wx.reLaunch({
                url: '/pages/index/index',
              })
              that.setData({
                msg: '点我登录'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  // 请假弹窗
  bindToRest: function() {
    var me = this;
    //判断是否绑定了孩子,拿出缓存中的childcode
    wx.getStorage({
      key: 'childcode',
      success: function(res) {
        console.log('拿到缓存中的childcode')
        console.log(res.data)
        var childcode = res.data;
        if (childcode == '' || childcode == null || childcode == undefined) {
          wx.redirectTo({
            url: '/pages/kid/kid',
          })
        } else {
          me.setData({
            showModal: true
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '请先绑定宝贝',
          icon: 'none'
        })
      }

    })
  },
  preventTouchMove: function() {},
  hideModal: function() {
    this.setData({
      showModal: false
    });
  },
  onCancel: function() {
    this.hideModal();
  },
  //获取昵称头像
  getUserInfo: function(e) {
    console.log(e)
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },
  // 请假表单
  formSubmit: function(e) {
    var me = this
    if (e.detail.value.childname == '') {
      wx.showToast({
        title: '请输入宝贝姓名!',
        icon: 'none',
        duration: 1500
      })
    } else if (e.detail.value.time == '') {
      wx.showToast({
        title: '请输入您的请假时间!',
        icon: 'none',
        duration: 1500
      })

    } else {
      var childname = e.detail.value.childname;
      var time = e.detail.value.time;
      var reason = e.detail.value.reason;
      console.log('姓名' + childname + '时间' + time + '原因' + reason)
      wx.request({
        url: app.serverUrl + '/leave', // 仅为示例，并非真实的接口地址
        method: 'POST',
        data: {
          childname: childname,
          time: time,
          reason: reason
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function(res) {
          console.log(res.data);
          if (res.data.status == 200) {
            wx.showToast({
              title: '请假成功',
              icon: 'success',
              duration: 1500
            })
            setTimeout(function() {
              me.hideModal();
            }, 1500)

          } else if (res.data.status == 500) {
            wx.showToast({
              title: '请假失败',
              duration: 1500
            })
          }

        }
      })
    }
  }

})